package com.sanley.piss.web.service;


import com.sanley.piss.mapper.ArchivesMapper;
import com.sanley.piss.mapper.DictionaryDataMapper;
import com.sanley.piss.model.ServiceImpl;
import com.sanley.piss.model.StringUtil;
import com.sanley.piss.model.exceptionUtil.exception.ServiceException;
import com.sanley.piss.model.exceptionUtil.json.ResultEnum;
import com.sanley.piss.model.po.Archives;
import com.sanley.piss.model.vo.ArchivesVO;
import com.sanley.piss.web.service.mapper.IArchivesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Create By  on 2021/3/13.
 *
 * @author 张凯大天才
 */
@Service
@Primary
public class ArchivesServiceImpl extends ServiceImpl<ArchivesMapper, Archives> implements IArchivesService {


    @Autowired
    private DictionaryDataMapper mapper;


    @Override
    public void insert(ArchivesVO archives, Integer userId) {
        if (StringUtil.isNotNull(baseMapper.selectById(userId))) {
            throw new ServiceException("不允许重复填写");
        }
        archives.setEmployeeId(userId);
        /**
         * 户口类型需要关联字段数据
         */
        archives.setAccountType(mapper.selectArchivesTypes(archives.getAccountTypeName(), "户口类型"));
        archives.setGmtCreate(LocalDateTime.now());
        baseMapper.insert(archives);
    }

    /**
     * 修改信息
     */
    @Override
    public void updateById(ArchivesVO archives) {
        archives.setAccountType(mapper.selectArchivesTypes(archives.getAccountTypeName(), "户口类型"));
        baseMapper.updateById(archives);
    }

    @Override
    public ArchivesVO selectById(Integer id) {
        ArchivesVO archives = baseMapper.selectById(id);
        if (StringUtil.isNull(archives)) {
            throw new ServiceException(ResultEnum.roleIsNull);
        }
        return archives;
    }

    @Override
    public void batchInsert(@Valid List<Archives> lists) {
        lists.forEach(list->{
            list.setGmtCreate(LocalDateTime.now());
        });
        baseMapper.batchInsert(lists);
    }

    @Override
    public List<ArchivesVO> selectAll() {
        return baseMapper.selectAll();
    }
}
